home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1997 #2 / Amiga Plus Extra 1997 #2.iso / pd / misc / envwrd41 / source / warpspell / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-16  |  5.7 KB  |  215 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  17. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  18. Prototype LibCall long            LibClose  (__A0 struct Library *);
  19. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  20.  
  21. struct Library *LibBase       = NULL;
  22. struct Library *IntuitionBase = NULL;
  23. struct Library *SysBase       = NULL;
  24. struct Library *DOSBase       = NULL;
  25. struct Library *DiskfontBase  = NULL;
  26. struct Library *RexxSysBase   = NULL;
  27. struct GfxBase *GfxBase       = NULL;
  28. struct GfxBase *ReqToolsBase  = NULL;
  29.  
  30. BPTR SegList = 0;
  31.  
  32. /*
  33.  *    The Initialization routine is given only a seglist pointer.  Since
  34.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  35.  *    and return either NULL or the library pointer.  Exec has Forbid()
  36.  *    for us during the call.
  37.  *
  38.  *    We use an extended library structure to allow identification as a
  39.  *    GoldED syntax scanner.
  40.  */
  41.  
  42. LibCall struct Library *
  43. LibInit(__D0 BPTR segment)
  44. {
  45.     struct Library *lib;
  46.  
  47.     static const long Vectors[] = {
  48.  
  49.         (long)ALibOpen,
  50.         (long)ALibClose,
  51.         (long)ALibExpunge,
  52.         (long)ALibReserved,
  53.  
  54.         (long)MountScanner,
  55.         (long)StartScanner,
  56.         (long)CloseScanner,
  57.         (long)FlushScanner,
  58.         (long)SetupScanner,
  59.         (long)BriefScanner,
  60.         (long)ParseLine,
  61.         (long)UnparseLines,
  62.         (long)ParseSection,
  63.         -1
  64.     };
  65.  
  66.     SysBase = (struct Library *)*(long *)4;
  67.  
  68.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  69.  
  70.         if (DiskfontBase = OpenLibrary("diskfont.library", 0)) {
  71.  
  72.             if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0)) {
  73.  
  74.                 if (IntuitionBase = OpenLibrary("intuition.library", 0)) {
  75.  
  76.                     if (RexxSysBase = OpenLibrary("rexxsyslib.library", 0)) {
  77.  
  78.                         if (ReqToolsBase = OpenLibrary("reqtools.library", 36)) {
  79.  
  80.                             if (InitC()) {
  81.  
  82.                                 if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct ParserBase), (BPTR)NULL)) {
  83.  
  84.                                     lib->lib_Node.ln_Type = NT_LIBRARY;
  85.                                     lib->lib_Node.ln_Name = LibName;
  86.                                     lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  87.                                     lib->lib_Version      = 37;
  88.                                     lib->lib_Revision     = 0;
  89.                                     lib->lib_IdString     = (APTR)LibId;
  90.  
  91.                                     ((struct ParserBase *)lib)->Magic = PARSER_MAGIC;
  92.  
  93.                                     SegList = segment;
  94.  
  95.                                     AddLibrary(lib);
  96.  
  97.                                     return(lib);
  98.                                 }
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     return(NULL);
  108. }
  109.  
  110. /*
  111.  *    Open is given the library pointer and the version request.  Either
  112.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  113.  *    Exec has Forbid() for us during the call.
  114.  */
  115.  
  116. LibCall struct Library *
  117. LibOpen(__D0 long version, __A0 struct Library *lib)
  118. {
  119.     ++lib->lib_OpenCnt;
  120.  
  121.     lib->lib_Flags &= ~LIBF_DELEXP;
  122.  
  123.     return(lib);
  124. }
  125.  
  126. /*
  127.  *    Close is given the library pointer and the version request.  Be sure
  128.  *    not to decrement the open count if already zero.  If the open count
  129.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  130.  *    and return the seglist.  Otherwise we return NULL.
  131.  *
  132.  *    Note that this routine never sets LIBF_DELEXP on its own.
  133.  *
  134.  *    Exec has Forbid() for us during the call.
  135.  */
  136.  
  137. LibCall long
  138. LibClose(__A0 struct Library *lib)
  139. {
  140.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  141.         return(NULL);
  142.  
  143.     if (lib->lib_Flags & LIBF_DELEXP)
  144.         return(LibExpunge(lib));
  145.  
  146.     return(NULL);
  147. }
  148.  
  149. /*
  150.  *    We expunge the library and return the Seglist ONLY if the open count
  151.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  152.  *    flag and return NULL.
  153.  *
  154.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  155.  *    might be called from the memory allocator and thus we CANNOT DO A
  156.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  157.  *
  158.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  159.  *    therefore freeze if we called it ourselves.  As far as I can tell
  160.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  161.  *    below.
  162.  */
  163.  
  164. LibCall long
  165. LibExpunge(__A0 struct Library *lib)
  166. {
  167.     if (lib->lib_OpenCnt) {
  168.  
  169.         lib->lib_Flags |= LIBF_DELEXP;
  170.         return(NULL);
  171.     }
  172.  
  173.     Remove(&lib->lib_Node);
  174.  
  175.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  176.  
  177.     if (ReqToolsBase) {
  178.  
  179.         CloseLibrary(ReqToolsBase);
  180.         ReqToolsBase = NULL;
  181.     }
  182.  
  183.     if (RexxSysBase) {
  184.  
  185.         CloseLibrary(RexxSysBase);
  186.         RexxSysBase = NULL;
  187.     }
  188.  
  189.     if (IntuitionBase) {
  190.  
  191.         CloseLibrary(IntuitionBase);
  192.         IntuitionBase = NULL;
  193.     }
  194.  
  195.     if (GfxBase) {
  196.  
  197.         CloseLibrary(GfxBase);
  198.         GfxBase = NULL;
  199.     }
  200.  
  201.     if (DiskfontBase) {
  202.  
  203.         CloseLibrary(DiskfontBase);
  204.         DiskfontBase = NULL;
  205.     }
  206.  
  207.     if (DOSBase) {
  208.  
  209.         CloseLibrary(DOSBase);
  210.         DOSBase = NULL;
  211.     }
  212.  
  213.     return((long)SegList);
  214. }
  215.